get.nws.fc <- function(temp.file = tempfile(), QPF = 24, lat = 40.80778,lon = -124.1611){
    
    options(warn=-1) # because coercing empty space to numeric gives a warning 

    url <- paste("http://www.wrh.noaa.gov/forecast/wxtables/index.php?",
                 paste("lat",lat,sep="="),
                 paste("lon",lon,sep="="),
                 "table=pfm",
                 "duration=7",
                 "interval=6",
                 sep="&")

    download.file(url,temp.file)
    str <- readLines(temp.file)
    li <- grep("QPF",str)
    str <- str[c(li,li+1)]
    str <- paste(str[1],str[2])
    rex <- '[colspan=4+colspan=3+a-z+A-Z+>+<+\t+=+\\/]'
    str <- gsub(rex, "", str)
    str <- strsplit(str,"  ")[[1]]
    str <- str[!is.na(as.numeric(str))]

    nws.fc <- as.numeric(str)
    if(QPF == 24)
        nws.fc <- apply(matrix(nws.fc,nrow=3,byrow=T),1,sum)
    nws.fc
}

get.grib.fc <- function(latlon = "40.80778,-124.1611", 
                    nfcdays = 3, 
                    grib.loc = "ftp://ftp.hpc.ncep.noaa.gov/grib/", 
                    save.path="./", 
                    degrib.path = "./"){

    grib.fc <- character(nfcdays)
    date <- Sys.Date()
    dateStr <- format(date,'%Y%m%d')
    
    for(i in 1:nfcdays){
        grib.name <- paste("p24m_",dateStr,"00f",
                            sprintf('%03i',(3-1)*24),".grb",sep='')
        grib.file <- tempfile() #paste(save.path,grib.name,sep='')
        if(!file.exists(grib.file)) 
                    download.file(paste(grib.loc,dateStr,
                                    "/",grib.name,sep=''),grib.file)

        grib.fc[i] <- system(paste(degrib.path,'degrib ',grib.file,
                                " -P -pnt ",latlon,sep=''),intern=T)[2]
        grib.fc[i] <- strsplit(grib.fc[i],',')[[1]][5]
    }
    grib.fc <- as.numeric(grib.fc)
    grib.fc
}
